package com.example.seed.listener;

import lombok.extern.slf4j.Slf4j;
import org.babyfish.jimmer.meta.ImmutableProp;
import org.babyfish.jimmer.sql.event.AssociationEvent;
import org.babyfish.jimmer.sql.event.EntityEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * 数据库更改侦听器
 *
 * @author admin
 * @date 2023/10/27
 */
@Component
public class DatabaseChangeListener {

    @EventListener
    public void onEntityChanged(EntityEvent<?> e) {
        System.out.println(e.getOldEntity() + "->" + e.getNewEntity());
    }

    @EventListener
    public void onAssociationChanged(AssociationEvent e) {
        ImmutableProp immutableProp = e.getImmutableProp();
        System.out.println("The many-to-one association `" + immutableProp + "` is changed");
        System.out.println("\tbook id: " + e.getSourceId());
        System.out.println("\tdetached book store id: " + e.getDetachedTargetId());
        System.out.println("\tattached book store id: " + e.getAttachedTargetId());

    }
}